package com.mlf.singleton.singleton.demo;

/**
 * 双检锁 - 线程安全
 */
public class DoubleCheckedSingleton {
    private static DoubleCheckedSingleton instance;

    private DoubleCheckedSingleton() {
    }

    public static DoubleCheckedSingleton getInstance() {
        if (instance == null) { // 第一次检查
            synchronized (DoubleCheckedSingleton.class) {
                if (instance == null) { // 第一次检查
                    instance = new DoubleCheckedSingleton();
                }
            }
        }
        return instance;
    }

    public void showMessage() {
        System.out.println("Hello from DoubleCheckedSingleton!");
    }

}
